/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.wdev.dbdevassist.action.base;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;

import top.wardyan.action.base.AbstractShell;
import top.wardyan.control.WButton;
import top.wardyan.control.WComposite;
import top.wardyan.control.WText;
import top.wardyan.interfaces.Resizeable;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.config.DDA;
import top.wardyan.wdev.dbdevassist.model.MasterModel;
import top.wardyan.wdev.dbdevassist.service.IMasterService;

/**
 * 2020-02-13 22:35:01
 * @author ward 2019年3月9日
 */
public abstract class BaseMasterEditShell<T extends BaseMasterEditShell<T, E>, E extends MasterModel<E>> extends AbstractShell<T> implements Resizeable{

	protected WComposite topCom;
	protected DDA dda = DDA.getInstance();
	protected IMasterService<E> masterService;
	protected E model;
	protected int sID;
	
	protected WButton btnAdd;
	protected WButton btnSave;
	protected WButton btnClear;
	protected WButton btnDisable;
	protected WButton btnMende;
	protected WButton btnCheck;
	protected WButton btnAudit;
	protected WButton btnLock;
	protected WButton btnClose;
	protected WButton btnDelete;
	protected WButton btnEdit;
	
	protected List<WText> texts = new ArrayList<WText>();
	
	protected ModifyListener modifyListener = new ModifyListener() {
		@Override
		public void modifyText(ModifyEvent e) {
			self.initBtnStatus(false);
		}
	};
	/**
	public BaseMasterEditShell() {
		this(0, DDA.getInstance().userService);
	}
	**/
	public BaseMasterEditShell(int sID, IMasterService<E> masterService) {
		super(SWT.SHELL_TRIM);
		this.sID = sID;
		this.masterService = masterService;
		if(sID > 0) {// 是编辑
			this.model = masterService.findByID(sID);
			if(this.model == null) {
				Msg.error(self, "未找到记录");
				self.dispose();
				return;
			}
		}
		
		this.createContents();
		this.addShellListener(new ShellAdapter() {
			@Override
			public void shellActivated(ShellEvent e) {
				self.initBtnStatus(false);
			}
		});
	}
	
	
	/**设置系统标题*/
	protected final String getTitle() {
		return DDA.SYSTEMCONFIG.SystemName + " " + DDA.SYSTEMCONFIG.BuildVersion + " " + title();
	}
	
	protected String title() {
		return "编辑";
	}


	private void createContents() {
		this.setLayout(new FormLayout());
		this.setSize(1024, 720);
		ShellUtil.center(this);
		
		topCom = new WComposite(this, SWT.BORDER);
		topCom.setLayout(new FormLayout());
		FormData fd_topCom = new FormData();
		//fd_topCom.height = 720;
		//fd_topCom.width = 1024;
		fd_topCom.bottom = new FormAttachment(0, 40);
		fd_topCom.right = new FormAttachment(100, -1);
		fd_topCom.top = new FormAttachment(0);
		fd_topCom.left = new FormAttachment(0);
		topCom.setLayoutData(fd_topCom);
		//FormLayout topLayout = new FormLayout();
		
		//topCom.setLayout(topLayout);
		int pad = 14;
		// 这个按钮不需要子类修改
		btnAdd = new WButton(topCom, SWT.NONE, "新增", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.add();
			}
		});
		FormAttachment btnHAttachment = new FormAttachment(0, 36);
		FormData fd_btnAdd = new FormData();
		fd_btnAdd.bottom = btnHAttachment;
		fd_btnAdd.right = new FormAttachment(btnAdd, pad * 3 + pad);
		fd_btnAdd.top = new FormAttachment(topCom, 2);
		fd_btnAdd.left = new FormAttachment(topCom, 10);
		btnAdd.setLayoutData(fd_btnAdd);
		
		btnSave = new WButton(topCom, SWT.NONE, "保存", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(self.save()) {
					self.sID = self.model.getSID();
					//Msg.show(self, "保存结果", "保存成功");
					self.initBtnStatus(true);
					//self.initTextStatus(true);
				}else {
					//Msg.error(self, "保存失败");
				}
				btnSave.setEnabled(false);
				btnEdit.setEnabled(true);
				btnClear.setEnabled(false);
			}
		});
		FormData fd_btnSave = new FormData();
		fd_btnSave.bottom = btnHAttachment;
		fd_btnSave.right = new FormAttachment(btnSave, pad * 3 + pad);
		fd_btnSave.top = new FormAttachment(topCom, 2);
		fd_btnSave.left = new FormAttachment(btnAdd, 10);
		btnSave.setLayoutData(fd_btnSave);
		
		btnClear = new WButton(topCom, SWT.NONE, "重置", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.clear();
				self.initBtnStatus(false);
				self.initTextStatus(true);
			}
		});
		FormData fd_btnClear = new FormData();
		fd_btnClear.bottom = btnHAttachment;
		fd_btnClear.right = new FormAttachment(btnClear, pad * 3 + pad);
		fd_btnClear.top = new FormAttachment(topCom, 2);
		fd_btnClear.left = new FormAttachment(btnSave, 10);
		btnClear.setLayoutData(fd_btnClear);
		
		btnDisable = new WButton(topCom, SWT.NONE, "废弃", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.disable();
			}
		});
		FormData fd_btnDisable = new FormData();
		fd_btnDisable.bottom = btnHAttachment;
		fd_btnDisable.right = new FormAttachment(btnDisable, pad * 3 + pad);
		fd_btnDisable.top = new FormAttachment(topCom, 2);
		fd_btnDisable.left = new FormAttachment(btnClear, 30);
		btnDisable.setLayoutData(fd_btnDisable);
		
		btnEdit = new WButton(topCom, SWT.NONE, "编辑", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.edit();
				btnEdit.setEnabled(false);
				btnMende.setEnabled(false);
			}
		});
		FormData fd_btnEdit = new FormData();
		fd_btnEdit.bottom = btnHAttachment;
		fd_btnEdit.right = new FormAttachment(btnEdit, pad * 3 + pad);
		fd_btnEdit.top = new FormAttachment(topCom, 2);
		fd_btnEdit.left = new FormAttachment(btnDisable, 10);
		btnEdit.setLayoutData(fd_btnEdit);
		
		
		btnMende = new WButton(topCom, SWT.NONE, "提交", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.mende();
			}
		});
		FormData fd_btnMende = new FormData();
		fd_btnMende.bottom = btnHAttachment;
		fd_btnMende.right = new FormAttachment(btnMende, pad * 3 + pad);
		fd_btnMende.top = new FormAttachment(topCom, 2);
		fd_btnMende.left = new FormAttachment(btnEdit, 10);
		btnMende.setLayoutData(fd_btnMende);
		
		btnCheck = new WButton(topCom, SWT.NONE, "复核", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.check();
			}
		});
		FormData fd_btnCheck = new FormData();
		fd_btnCheck.bottom = btnHAttachment;
		fd_btnCheck.right = new FormAttachment(btnCheck, pad * 3 + pad);
		fd_btnCheck.top = new FormAttachment(topCom, 2);
		fd_btnCheck.left = new FormAttachment(btnMende, 10);
		btnCheck.setLayoutData(fd_btnCheck);
		
		btnAudit = new WButton(topCom, SWT.NONE, "审核", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.audit();
			}
		});
		FormData fd_btnAudit = new FormData();
		fd_btnAudit.bottom = btnHAttachment;
		fd_btnAudit.right = new FormAttachment(btnAudit, pad * 3 + pad);
		fd_btnAudit.top = new FormAttachment(topCom, 2);
		fd_btnAudit.left = new FormAttachment(btnCheck, 10);
		btnAudit.setLayoutData(fd_btnAudit);
		
		btnLock = new WButton(topCom, SWT.NONE, "锁定", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.lock();
			}
		});
		FormData fd_btnLock = new FormData();
		fd_btnLock.bottom = btnHAttachment;
		fd_btnLock.right = new FormAttachment(btnLock, pad * 3 + pad);
		fd_btnLock.top = new FormAttachment(topCom, 2);
		fd_btnLock.left = new FormAttachment(btnAudit, 10);
		btnLock.setLayoutData(fd_btnLock);
		
		btnDelete = new WButton(topCom, SWT.NONE, "删除", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.delete();
			}
		});
		FormData fd_btnDelete = new FormData();
		fd_btnDelete.bottom = btnHAttachment;
		fd_btnDelete.right = new FormAttachment(btnDelete, pad * 3 + pad);
		fd_btnDelete.top = new FormAttachment(topCom, 2);
		fd_btnDelete.left = new FormAttachment(btnLock, 10);
		btnDelete.setLayoutData(fd_btnDelete);
		
		btnClose = new WButton(topCom, SWT.NONE, "关闭", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.close();
				self.dispose();
			}
		});
		FormData fd_btnClose = new FormData();
		fd_btnClose.bottom = btnHAttachment;
		fd_btnClose.right = new FormAttachment(btnClose, pad * 3 + pad);
		fd_btnClose.top = new FormAttachment(topCom, 2);
		fd_btnClose.left = new FormAttachment(btnDelete, 10);
		btnClose.setLayoutData(fd_btnClose);
		
		//btnAdd
		//btnSave.setEnabled(false);
		btnSave.setEnabled(false);
		btnClear.setEnabled(false);
		btnDisable.setEnabled(false);
		btnEdit.setEnabled(false);
		btnCheck.setEnabled(false);
		btnMende.setEnabled(false);
		btnAudit.setEnabled(false);
		btnLock.setEnabled(false);
		btnDelete.setEnabled(false);
		
		this.initBtnStatus(true);
	}
	
	/** 检查编辑栏里的数据有没有发生过变动了. 如果 **/
	protected abstract boolean checkModelChange();
	
	// sID > 0表示是编辑, = 0是新增
	
	protected void initBtnStatus(boolean isInit) {
		boolean checkchange = false;
		if(!isInit) {
			checkchange = this.checkModelChange();
		}
		if((sID == 0 && checkchange) || (sID > 0 && !isInit && checkchange)) {
			btnSave.setEnabled(true);
			btnClear.setEnabled(true);
		}else {
			btnSave.setEnabled(false);
			btnClear.setEnabled(false);
		}
		// btnDisable btnMende btnCheck btnAudit btnLock btnDelete
		if(sID > 0) {
			int status = model.getSStatus().getSID();
			if(checkchange) {
				btnDisable.setEnabled(false);
				btnEdit.setEnabled(false);
				btnMende.setEnabled(false);
				btnCheck.setEnabled(false);
				btnAudit.setEnabled(false);
				btnLock.setEnabled(false);
				btnDelete.setEnabled(false);
			}else if(status == 1) {
				btnDisable.setEnabled(false);
				btnEdit.setEnabled(true);
				btnMende.setEnabled(false);
				btnCheck.setEnabled(false);
				btnAudit.setEnabled(false);
				btnLock.setEnabled(false);
				btnDelete.setEnabled(true);
			}else if(status == 2) {	// 编辑状态下:
				btnDisable.setEnabled(true);
				btnEdit.setEnabled(!btnSave.getEnabled());
				btnMende.setEnabled(true);
				btnCheck.setEnabled(false);
				btnAudit.setEnabled(false);
				btnLock.setEnabled(false);
				btnDelete.setEnabled(true);
			}else if(status == 3) {
				btnDisable.setEnabled(false);
				btnEdit.setEnabled(true);
				btnMende.setEnabled(false);
				btnCheck.setEnabled(true);
				btnAudit.setEnabled(false);
				btnLock.setEnabled(false);
				btnDelete.setEnabled(false);
			}else if(status == 4) {
				btnDisable.setEnabled(false);
				btnEdit.setEnabled(false);
				btnMende.setEnabled(true);
				btnCheck.setEnabled(false);
				btnAudit.setEnabled(true);
				btnLock.setEnabled(false);
				btnDelete.setEnabled(false);
			}else if(status == 5) {
				btnDisable.setEnabled(false);
				btnEdit.setEnabled(false);
				btnMende.setEnabled(false);
				btnCheck.setEnabled(true);
				btnAudit.setEnabled(false);
				btnLock.setEnabled(true);
				btnDelete.setEnabled(false);
			}else if(status == 6) {
				btnDisable.setEnabled(false);
				btnEdit.setEnabled(false);
				btnMende.setEnabled(false);
				btnCheck.setEnabled(false);
				btnAudit.setEnabled(true);
				btnLock.setEnabled(false);
				btnDelete.setEnabled(false);
			}
		}else {
			btnDisable.setEnabled(false);
			btnEdit.setEnabled(false);
			btnMende.setEnabled(false);
			btnCheck.setEnabled(false);
			btnAudit.setEnabled(false);
			btnDelete.setEnabled(false);
			btnLock.setEnabled(false);
		}
		
		// 保存按钮可用时, 编辑~删除按钮全部禁用
		// 只有处于编辑状态下, 所有输入框和下拉框才能编辑 // 单选和多选未支持
		initTextStatus(isInit);
	}
	
	protected void initTextStatus(boolean isInit) {
		if(texts != null) {
			for(WText w : texts) {
				//w.setEditable(sID == 0 || (btnEdit.getEnabled() && btnMende.getEnabled()) || !(!btnEdit.getEnabled() && (btnMende.getEnabled() || btnCheck.getEnabled() || btnAudit.getEnabled() || btnLock.getEnabled())));
				if(sID == 0) {	// 新增状态下肯定都可以编辑
					w.setEditable(true);
				}else {
					//btnEdit=true时也不行(此时是审核
					if(isInit) {
						w.setEditable(false);
					}else {
					// btnEdit=false时,区分情况: 是当前正在编辑,还是已提交
						if(model.getSStatus().getSID() == 2) {
							w.setEditable(true);
							//w.setEditable(false);
						}else {
							w.setEditable(false);
						}
					}
				}
				
			}
		}
	}
	
	protected void enableBtn(WButton...btns){
		if(btns != null) {
			for(WButton b : btns) {
				b.setEnabled(true);
			}
		}
	}
	
	/**
	 * 新增
	 */
	protected void add() {
		//self.dispose();
		//new BaseMasterEditShell(sID, masterService);
		sID = 0;
		model = null;
		this.initBtnStatus(true);
		show();
	}
	/**
	 * 保存
	 */
	protected abstract boolean save();
	/**
	 * 提交
	 */
	protected void mende() {
		if(self.checkServiceAndModel()) {
			model.setSStatus(3);
			updateModel();
		}
	}
	/**
	 * 复核
	 */
	protected void check() {
		if(self.checkServiceAndModel()) {
			model.setSStatus(4);
			updateModel();
		}
	}
	/**
	 * 审核
	 */
	protected void audit() {
		if(self.checkServiceAndModel()) {
			model.setSStatus(5);
			updateModel();
		}
	}
	/**
	 * 删除
	 */
	protected void delete() {
		if(Msg.confirm(self, "警告", "删除操作不可恢复,确定?", SWT.OK | SWT.CANCEL) == SWT.OK) {
			if(self.checkServiceAndModel()) {
				masterService.delete(model);
			}
			self.dispose();
		}
	}
	
	protected boolean checkServiceAndModel() {
		boolean rs = true;
		if(masterService == null) {
			rs = false;
			Msg.error(self, "服务为空");
		}else if(model == null) {
			rs = false;
			Msg.error(self, "MODEL为空");
		}
		return rs;
	}
	/**
	 * 废弃
	 */
	protected void disable() {
		if(self.checkServiceAndModel()) {
			//model.setSStatus(1).update();
			model.setSStatus(1);
			updateModel();
			// 刷新
		}
	}
	
	protected void updateModel() {
		// model.update();
		masterService.update(model);
		this.initBtnStatus(false);
	}
	/**
	 * <p>编辑</p>
	 * <p>注意, 当原状态IN(1, 5, 6)时只有所有者能改变状态<p>
	 * <p>用户列表时需要重写本方法, 只允许修改自己的, 以及默认的管理员允许修改所有</p>
	 */
	protected void edit() {
		if(self.checkServiceAndModel()) {
			if((model.getSStatus().getSID() == 1 
					|| model.getSStatus().getSID() == 5 
					|| model.getSStatus().getSID() == 6)
				&& (model.getROwnerID().getSID() != dda.user.getSID())) {
				Msg.error(self, "权限不足");
			}else {
				model.setSStatus(2);
				updateModel();
			}
		}
	}
	
	/**
	 * 重置
	 */
	//protected abstract void reset();
	/**
	 * 清空
	 */
	protected void clear() {
		this.show();
		self.initBtnStatus(false);
	}
	
	protected void lock() {
		if(self.checkServiceAndModel()) {
			model.setSStatus(6);
			updateModel();
		}
	}
	
	/** 显示 **/
	protected abstract void show();
}
